// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package gui.utils
{
    import com.bit101.components.PushButton;

    import flash.display.DisplayObjectContainer;
    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.events.Event;
    import flash.text.TextField;

    public class ConfirmBox
    {
        private static var _instance:ConfirmBox;

        public static function get instance():ConfirmBox
        {
            if (_instance == null)
            {
                _instance = new ConfirmBox();
            }
            return _instance;
        }

        private var mLayerContainer:DisplayObjectContainer;

        //防止点击事件穿透
        private var mBackGroundLayerShape:Shape;
        private var mConfirmBoxMc:MovieClip;

        private var mYesCallback:Function;
        private var mYesButton:PushButton;

        private var mNoButton:PushButton;
        private var mNoCallback:Function;

        private var mTitleTF:TextField;


        public function ConfirmBox()
        {
        }

        //==================================
        //== Initialize & Dispose
        //==================================

        public function initialize(_layerContainer:DisplayObjectContainer, _ConfirmBoxClass:Class):void
        {
            mLayerContainer = _layerContainer;
            mConfirmBoxMc = new _ConfirmBoxClass() as MovieClip;
            mBackGroundLayerShape = new Shape();
            mBackGroundLayerShape.graphics.beginFill(0x0, 0.2);
            mBackGroundLayerShape.graphics.drawRect(0,
                                                    0,
                                                    _layerContainer.stage.stageWidth,
                                                    _layerContainer.stage.stageHeight);

            _layerContainer.addChild(mBackGroundLayerShape);

            mConfirmBoxMc.x = _layerContainer.stage.stageWidth / 2;
            mConfirmBoxMc.y = _layerContainer.stage.stageHeight / 2;
            _layerContainer.addChild(mConfirmBoxMc);

            mYesButton = new PushButton(null, 0, 0, "Yes", onClickYesButton);
            GUIUtils.replaceTargetWithPH(mYesButton, mConfirmBoxMc["yesBtnPH"]);

            mNoButton = new PushButton(null, 0, 0, "No", onClickNoButton);
            GUIUtils.replaceTargetWithPH(mNoButton, mConfirmBoxMc["noBtnPH"]);

            mTitleTF = mConfirmBoxMc["titleTF"];

            mLayerContainer.visible = false;
        }

        //==================================
        //== Public Function
        //==================================

        public function ask(_msg:String, _yesCallback:Function, _noCallback:Function = null):void
        {
            mLayerContainer.visible = true;
            mTitleTF.text = _msg;
            mYesCallback = _yesCallback;
            mNoCallback = _noCallback;
        }

        //==================================
        //== Private Function
        //==================================
        private function onClickYesButton(e:Event):void
        {
            mYesCallback && mYesCallback();
            close();
        }

        private function onClickNoButton(e:Event):void
        {
            mNoCallback && mNoCallback();
            close();
        }

        private function close():void
        {
            mYesCallback = mNoCallback = null;
            mLayerContainer.visible = false;
        }

    }
}
